<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>

</#if>
<#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
    </resultMap>

</#if>
<#if baseColumnList>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
<#list table.commonFields as field>
        ${field.columnName}<#sep>,</#sep>
</#list>
    </sql>

    <!-- 完整查询结果列 -->
    <sql id="Full_Column_List">
<#list table.fields as field>
        ${field.columnName}<#sep>,</#sep>
</#list>
    </sql>

    <!-- 插入字段列 -->
    <sql id="Insert_Column_List">
<#list table.fields as field>
<#if !field.keyIdentityFlag>
        ${field.columnName}<#sep>,</#sep>
</#if>
</#list>
    </sql>

    <!-- 查询条件 -->
    <sql id="Where_Clause">
        <where>
<#list table.fields as field>
<#if field.propertyType == "String">
            <if test="entity.${field.propertyName} != null and entity.${field.propertyName} != ''">
                AND ${field.columnName} = ${r'#{entity.'}${field.propertyName}${r'}'}
            </if>
<#else>
            <if test="entity.${field.propertyName} != null">
                AND ${field.columnName} = ${r'#{entity.'}${field.propertyName}${r'}'}
            </if>
</#if>
</#list>
        </where>
    </sql>

</#if>

    <!-- 根据ID查询详情 -->
    <select id="selectByIdWithDetails" resultMap="BaseResultMap">
        SELECT
        <include refid="Full_Column_List"/>
        FROM ${table.name}
        WHERE <#list table.fields as field><#if field.keyFlag>${field.columnName} = ${r'#{id}'}</#if></#list>
    </select>

    <!-- 分页查询列表 -->
    <select id="selectPageList" resultMap="BaseResultMap">
        SELECT
        <include refid="Full_Column_List"/>
        FROM ${table.name}
        <include refid="Where_Clause"/>
        ORDER BY 
<#list table.fields as field>
<#if field.keyFlag>
        ${field.columnName} DESC
</#if>
</#list>
    </select>

    <!-- 查询列表 -->
    <select id="selectList" resultMap="BaseResultMap">
        SELECT
        <include refid="Full_Column_List"/>
        FROM ${table.name}
        <include refid="Where_Clause"/>
        ORDER BY 
<#list table.fields as field>
<#if field.keyFlag>
        ${field.columnName} DESC
</#if>
</#list>
    </select>

    <!-- 批量插入 -->
    <insert id="insertBatch">
        INSERT INTO ${table.name} (
        <include refid="Insert_Column_List"/>
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
<#list table.fields as field>
<#if !field.keyIdentityFlag>
            ${r'#{item.'}${field.propertyName}${r'}'}<#sep>,</#sep>
</#if>
</#list>
            )
        </foreach>
    </insert>

    <!-- 批量更新 -->
    <update id="updateBatch">
        <foreach collection="list" item="item" separator=";">
            UPDATE ${table.name}
            <set>
<#list table.fields as field>
<#if !field.keyFlag>
<#if field.propertyType == "String">
                <if test="item.${field.propertyName} != null and item.${field.propertyName} != ''">
                    ${field.columnName} = ${r'#{item.'}${field.propertyName}${r'}'},
                </if>
<#else>
                <if test="item.${field.propertyName} != null">
                    ${field.columnName} = ${r'#{item.'}${field.propertyName}${r'}'},
                </if>
</#if>
</#if>
</#list>
            </set>
            WHERE <#list table.fields as field><#if field.keyFlag>${field.columnName} = ${r'#{item.'}${field.propertyName}${r'}'}</#if></#list>
        </foreach>
    </update>

    <!-- 根据条件删除 -->
    <delete id="deleteByCondition">
        DELETE FROM ${table.name}
        <include refid="Where_Clause"/>
    </delete>

    <!-- 统计数量 -->
    <select id="countByCondition" resultType="long">
        SELECT COUNT(1)
        FROM ${table.name}
        <include refid="Where_Clause"/>
    </select>

</mapper>